import { useRequest } from 'umi';
import {
  queryAreaListAPI,
  queryDoorListAPI,
  addDoorAPI,
  queryPlanModeAPI,
  queryDoorInfoAPI,
  editDoorAPI,
  deleteDoorAPI,
  queryControllerTypeAPI,
  queryDoorOpenTypeAPI,
  sentDownPlanModeAPI,
  queryUserListAPI,
  configPermissionAPI,
  queryCardUserListByDoorAPI,
  queryDoorListByUserAPI,
  queryDoorConfigPermissionRecordAPI,
  queryUserConfigPermissionRecordAPI,
  deletePermissionAPI,
  addPermissionAPI,
  isConfigAPI,
} from './services';
import { createModel } from 'hox';
import { useState } from 'react';
import { DataNode } from 'antd/lib/tree';
import { AreaListReturnProps } from './API';
import { usePersistFn } from 'ahooks';
type AreaListProps = DataNode & { code?: string };
interface HoxModelProps {
  areaList: AreaListProps[];
  selectedKeys: string[];
  currentCode: string;
  selectedRowKeys: (string | number)[];
  formAreaList: AreaListReturnProps[];
  userFormAreaList: AreaListReturnProps[];
  currentPageType: string;
  info: { id?: number; [key: string]: any };
}
function useHoxModel() {
  const [state, setState] = useState<HoxModelProps>({
    areaList: [],
    selectedKeys: [],
    currentCode: '',
    selectedRowKeys: [],
    formAreaList: [],
    userFormAreaList: [],
    currentPageType: 'user',
    info: {},
  });
  const dispatch = (payload: Partial<HoxModelProps>) => {
    setState((pre) => ({ ...pre, ...payload }));
  };

  //获取门设备列表接口
  const { run: getFormDoorList, data: formDoorList } = useRequest(queryDoorListAPI, {
    manual: true,
  });
  //获取用户表单区域接口
  const { run: getUserFormAreaList } = useRequest(queryAreaListAPI, {
    manual: true,
    onSuccess: (res) => {
      if (res.result === 0) {
        let data = res?.data;
        const userFormAreaList = data.map((item: any) => {
          return {
            label: item.name,
            value: item.id,
            isLeaf: item.isRegion,
            code: item.code,
          };
        });
        dispatch({ userFormAreaList });
      }
    },
    formatResult: (res) => res,
  });
  //获取表单区域接口
  const { run: getFormAreaList } = useRequest(queryAreaListAPI, {
    manual: true,
    onSuccess: (res) => {
      if (res.result === 0) {
        let data = res?.data;
        const formAreaList = data.map((item: any) => {
          return {
            label: item.name,
            value: item.id,
            isLeaf: item.isRegion,
            code: item.code,
          };
        });
        dispatch({ formAreaList });
      }
    },
    formatResult: (res) => res,
  });
  //下发计划模板
  const { run: sentDownPlanMode } = useRequest(sentDownPlanModeAPI, {
    manual: true,
  });
  //下发计划模板
  const { run: configPermission } = useRequest(configPermissionAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取控制器类型接口
  const { run: getControllerType, data: controllerType } = useRequest(queryControllerTypeAPI, {
    manual: true,
  });
  //获取控制器类型接口
  const { run: getDoorOpenType, data: doorOpenType } = useRequest(queryDoorOpenTypeAPI, {
    manual: true,
  });
  //获取计划模板列表接口
  const { run: getPlanMode, data: planMode } = useRequest(queryPlanModeAPI, {
    manual: true,
  });
  //根据用户id查询所有拥有权限的门
  const { run: getDoorListByUser, data: doorListByUser } = useRequest(queryDoorListByUserAPI, {
    manual: true,
  });
  //根据门id查询所有绑定卡片信息的用户
  const { run: getCardUserListByDoor, data: cardUserListByDoor } = useRequest(
    queryCardUserListByDoorAPI,
    {
      manual: true,
    },
  );
  //获取左边区域接口
  const { run: getAreaList, loading: getAreaListLoading } = useRequest(queryAreaListAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //判断有没有权限添加
  const { run: isConfig } = useRequest(isConfigAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //添加门接口
  const { run: addDoor } = useRequest(addDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //删除用户接口
  const { run: deleteDoor } = useRequest(deleteDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //修改用户接口
  const { run: editDoor } = useRequest(editDoorAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取用户详情接口
  const { run: getDoorInfo, data: doorInfo, loading: doorInfoLoading } = useRequest(
    queryDoorInfoAPI,
    {
      manual: true,
    },
  );
  //获取门设备列表接口
  const { run: getDoorList, data: doorList } = useRequest(queryDoorListAPI, {
    manual: true,
  });
  //添加权限
  const { run: addPermission } = useRequest(addPermissionAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //删除权限
  const { run: deletePermission } = useRequest(deletePermissionAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取用户列表
  const { run: getUserList, data: userList } = useRequest(queryUserListAPI, {
    manual: true,
  });
  //根据用户id获取门的权限记录
  const { run: getDoorConfigPermissionRecord } = useRequest(queryDoorConfigPermissionRecordAPI, {
    manual: true,
  });
  //根据门id获取用户的权限记录
  const { run: getUserConfigPermissionRecord } = useRequest(queryUserConfigPermissionRecordAPI, {
    manual: true,
  });

  //第一次获取区域
  const firstGetAreaList = usePersistFn(async () => {
    let currentArea = await getAreaList();
    if (currentArea && currentArea.result === 0) {
      //数据格式转换
      const areaList = currentArea.data.map((item: any) => {
        return {
          key: item.id.toString(),
          title: item.name,
          isLeaf: item.isRegion,
          code: item.code,
        };
      });
      //默认选中第一个
      let item = areaList[0];

      dispatch({ areaList, selectedKeys: [item.key], currentCode: item.code });
    }
  });
  const setFormList = usePersistFn((data) => {
    dispatch({
      formAreaList:
        data?.map((item: any) => {
          return {
            label: item.name || item.label,
            value: item.id || item.value,
            isLeaf: item.isRegion || item.isLeaf,
            code: item.code,
          };
        }) || [],
    });
  });
  return {
    ...state,
    dispatch,
    getUserList,
    userList,
    setFormList,
    addDoor,
    getAreaList,
    editDoor,
    isConfig,
    getDoorList,
    configPermission,
    doorList,
    getDoorInfo,
    doorInfo,
    deleteDoor,
    doorInfoLoading,
    getPlanMode,
    getControllerType,
    controllerType,
    getFormDoorList,
    formDoorList,
    doorOpenType,
    getDoorOpenType,
    sentDownPlanMode,
    getUserFormAreaList,
    planMode,
    getFormAreaList,
    firstGetAreaList,
    getAreaListLoading,
    getCardUserListByDoor,
    getDoorListByUser,
    getDoorConfigPermissionRecord,
    getUserConfigPermissionRecord,
    doorListByUser,
    cardUserListByDoor,
    deletePermission,
    addPermission,
  };
}
export default createModel(useHoxModel);
